package com.mf.basic.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.mf.basic.debounces.Debounce;
import com.mf.basic.entity.DtoUser;
import com.mf.basic.log_api.LogApi;
import com.mf.basic.services.ServiceUser;
import com.mf.basic.utils.MyAssert;
import com.mf.basic.rs.ResponseResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Luke on 2025/3/7 16:58.
 */
@RestController
@ResponseResult
@RequestMapping("/user")
public class
ControllerUser implements MyAssert {

    @Resource
    private ServiceUser serviceUser;

    /**
     * 注册用户
     */
    @Debounce
    @LogApi("用户-注册")
    @PostMapping("/register")
    @SaCheckPermission("user.register")
    public void register(@RequestBody DtoUser user) {
        String username = strIsNull(user.getUsername(), "用户名");
        String password = strIsNull(user.getPassword(), "密码");
        Assert.state(username.length() >= 4, "用户名长度不能小于4, 请重新输入");
        String nick = strIsNull(user.getNick(), "昵称");
        serviceUser.doAdd(user);
    }

    /**
     * 分页查询用户
     *
     * @param params key,roleId,pageNum,pageSize
     * @return IPage<DtoUser>
     */
    @SaCheckPermission("user.page")
    @PostMapping("/page")
    public IPage<DtoUser> page(@RequestBody Map<String, Object> params) {
        pageIsNull(params);
        params.remove("roleId");
        params.remove("neRoleId");
        return serviceUser.findPageByParams(params);
    }

    /**
     * 不分页查询用户
     */
    @SaCheckPermission("user.page")
    @PostMapping("/list")
    public List<DtoUser> list(@RequestBody Map<String, Object> params) {
        pageIsNull(params);
        params.remove("roleId");
        return serviceUser.findListByParams(params);
    }

    /**
     * 启用用户
     *
     * @param params id
     */
    @LogApi("用户-启用")
    @SaCheckPermission("user.manager")
    @PostMapping("/enable")
    public void doEnable(@RequestBody Map<String, Object> params) {
        long id = idIsNull(params.get("id"), "ID");
        serviceUser.upStatus(id, 0);
    }

    /**
     * 禁用用户
     *
     * @param params id
     */
    @LogApi("用户-禁用")
    @SaCheckPermission("user.manager")
    @PostMapping("/disable")
    public void doDisable(@RequestBody Map<String, Object> params) {
        long id = idIsNull(params.get("id"), "ID");
        serviceUser.upStatus(id, 1);
        StpUtil.login(id);
    }

    /**
     * 用户修改密码
     *
     * @param params id,oldPassword,newPassword
     */
    @LogApi("用户-修改密码(用户)")
    @Debounce
    @PostMapping("/upPasswordUser")
    public void upPasswordUser(@RequestBody Map<String, Object> params) {
        String oldPassword = strIsNull(params.get("oldPassword"), "旧密码");
        String newPassword = strIsNull(params.get("newPassword"), "新密码");
        serviceUser.upPasswordUser(oldPassword, newPassword);
    }

    /**
     * 管理员修改用户密码
     *
     * @param params id,newPassword
     */
    @LogApi("用户-修改密码(管理员)")
    @Debounce
    @SaCheckPermission("user.upPasswordManager")
    @PostMapping("/upPasswordManager")
    public void upPasswordManager(@RequestBody Map<String, Object> params) {
        long id = idIsNull(params.get("id"), "ID");
        String newPassword = strIsNull(params.get("newPassword"), "新密码");
        serviceUser.upPasswordManager(id, newPassword);
    }

    /**
     * 修改用户信息
     *
     * @param user id,nick,remark
     */
    @LogApi("用户-修改信息")
    @Debounce
    @PostMapping("/update")
    public void update(@RequestBody DtoUser user) {
        idIsNull(user.getId(), "ID");
        strIsNull(user.getNick(), "昵称");
        serviceUser.doUpdate(user);
    }

    /**
     * 查询用户权限
     *
     * @return List<String>
     */
    @Debounce
    @GetMapping("/findPermission")
    public List<String> findPermission() {
        return StpUtil.getPermissionList();
    }

    /**
     * 强制注销指定用户
     *
     * @param params 用户id
     */
    @LogApi("用户-强制注销")
    @Debounce
    @SaCheckPermission("set.logout")
    @PostMapping("/logout")
    public void logout(@RequestBody Map<String, Object> params) {
        long id = idIsNull(params.get("id"), "ID");
        StpUtil.login(id);
    }

}
